package main

import (
	"log"
	"fmt"
	"net/http"
	"github.com/gorilla/websocket"
)

var upgrader=websocket.Upgrader{}
func home(w http.ResponseWriter,r *http.Request){
	//这个是服务端发送出去的
	http.ServeFile(w,r,"home.html")
	log.Print(w,r)
	fmt.Println(w,r)
}

func echo(w http.ResponseWriter,r *http.Request){
	c,_:=upgrader.Upgrade(w,r,nil)
	defer c.Close()
	for {  // 收到send后的处理 多个客户端订阅的话，每个客户端都会收到同样的消息，这个是广播机制
		mt,message,_:=c.ReadMessage()
		c.WriteMessage(mt,append([]byte("发送"),message[:]...))
	}
}

func main(){
	log.Print("hasasasasas ki")
	log.Println("陈常鸿")
	log.Printf("余佩玲")
	//hasaki()

	http.HandleFunc("/echo",echo)    // 接收到的信息
	http.HandleFunc("/",home)        // home用于把信息发到客户端
	log.Fatal(http.ListenAndServe("127.0.0.1:6666",nil))
}